/*
 * @Author: your name
 * @Date: 2021-01-03 22:50:01
 * @LastEditTime: 2021-01-03 23:53:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\H5 2010 练习\第三阶段 Vue + node\DAY 7\panda\src\store\plugins\user.js
 */
export const userPlugin = store => {
  // 为 store 添加监听，在每次 mutation 调用后执行回调函数
  store.subscribe((mutation, state) => {
    // console.log('userPlugin:', mutation)
    switch (mutation.type) {
      case 'user/loginSuccess': { // 登陆成功，将用户数据缓存到 webStorage 中
        const { userInfo, token, remember } = mutation.payload
        const storage = remember ? localStorage : sessionStorage
        storage.setItem('userInfo', JSON.stringify(userInfo))
        storage.setItem('token', token)
        break
      }
      case 'user/logoutSuccess': { // 登陆成功，将用户数据缓存从 webStorage 中删除
        localStorage.clear()
        sessionStorage.clear()
        break
      }
    }
  })
}
